package simple17;

import simple17.factory.FruitFactory;
import simple17.fruit.Fruit;
import simple17.visit.Visit;

import java.util.ArrayList;
import java.util.List;

/**
 * 访问者模式
 *  利用双重分派机制，弥补java多态中的方法重载是静态化的不足
 */
public class Simple {
    static List<Fruit> list = new ArrayList<>();
    static Visit visit = new Visit();

    public static void main(String[] args) {
        list.add(FruitFactory.getApple());
        list.add(FruitFactory.getOrange());
        list.add(FruitFactory.getBanana());

        price();
    }

    private static int price() {
        int total = 0;
        for (Fruit fruit : list) {
//            total += visit.sell(fruit);  //java 方法重载无法识别真实类型
            total += fruit.accept(visit);
        }
        System.out.println("总价值" + total);
        return total;
    }
}
